

function Uasit_UpdateMember()
	local onlinemember=0;
	local offlinemember=0;
	if(TEST_PARTY_STUFF and getn(TEST_PARTY_STUFF) > 0) then
		totalMembers = getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"]);
	else
		for i=1, UDKP_NUM_BIDS_TO_DISPLAY  do
			getglobal("UdkpMemberFrameListButton"..i):Hide();
		end
		FauxScrollFrame_Update(UdkpMemberListScrollFrame,0 ,UDKP_NUM_BIDS_TO_DISPLAY,AUCTIONS_BUTTON_HEIGHT);
		return;
	end
	FauxScrollFrame_Update(UdkpMemberListScrollFrame,totalMembers ,UDKP_NUM_BIDS_TO_DISPLAY,AUCTIONS_BUTTON_HEIGHT);
	
	local offset = FauxScrollFrame_GetOffset(UdkpMemberListScrollFrame);
	for i=1, UDKP_NUM_BIDS_TO_DISPLAY  do
		k = offset + i;
		if ( k > totalMembers ) then
			getglobal("UdkpMemberFrameListButton"..i):Hide();
		else
			v = TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][k];
			local hour = string.format("%d", v["totaltime"]/3600);
			local minute = string.format("%d", (v["totaltime"] - hour * 3600) / 60 );
			getglobal("UdkpMemberFrameListButton"..i):Show();
			
			if(v["zone"]) then
			
			else
				v["zone"]=UDKP_MESSAGE_OFFLINE;
			end
			if ( getglobal("UdkpMemberFrameListButton" .. i) ) then
			
				if(v["online"]==1) then
					
					getglobal("UdkpMemberFrameListButton" .. i .. "Name1"):SetText(v["player"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Name"):SetText(v["player"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Class"):SetText(v["class"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Level"):SetText(v["level"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Cdkp"):SetText(v["dkp"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Ldkp"):SetText(v["pdkp"]+v["dkp"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Pdkp"):SetText(v["pdkp"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Map"):SetText(v["zone"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "JoinTime"):SetText(v["join"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "TotalTime"):SetText(hour..UDKP_MESSAGE_HOUR..minute..UDKP_MESSAGE_MINUTE);
				else
					
					getglobal("UdkpMemberFrameListButton" .. i .. "Name1"):SetText(v["player"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Name"):SetText("|cff9d9d9d"..v["player"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Class"):SetText("|cff9d9d9d"..v["class"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Level"):SetText("|cff9d9d9d"..v["level"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Cdkp"):SetText("|cff9d9d9d"..v["dkp"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Ldkp"):SetText("|cff9d9d9d"..(v["pdkp"]+v["dkp"]));
					getglobal("UdkpMemberFrameListButton" .. i .. "Pdkp"):SetText("|cff9d9d9d"..v["pdkp"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "Map"):SetText("|cff9d9d9d"..v["zone"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "JoinTime"):SetText("|cff9d9d9d"..v["join"]);
					getglobal("UdkpMemberFrameListButton" .. i .. "TotalTime"):SetText("|cff9d9d9d"..hour..UDKP_MESSAGE_HOUR..minute..UDKP_MESSAGE_MINUTE);
				end
			end
		end
	end
	
	for i=1, totalMembers  do
		v = TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]
		if(v["online"]==1) then
			onlinemember=onlinemember+1;
		else
			offlinemember=offlinemember+1;
		end
	end
	getglobal("UdkpRaidHeaderString"):SetText(UDKP_MESSAGE_MEMBER.."|cff1eff00"..TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Notes"].." |r".."(|cffffffff"..totalMembers.."|r:".."|cff1eff00"..onlinemember.."|r/|cff9d9d9d"..offlinemember.."|r)");
	
end

function Udkp_Member_Compute()
	
	

	if( CurrentRaidId==0 or TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]~=1) then
		
		Uasit_UpdateMember();
		return;
	end
	
	Uasit_Debug("GetNumRaidMembers()",GetNumRaidMembers())
			
	for i = 1, GetNumRaidMembers(), 1 do
		local sPlayer = UnitName("raid" .. i);
		local _, race = UnitRace("raid" .. i);
		
		if(sPlayer and sPlayer ~= UdkpRaid_lang_UnknownEntity) then
			local name, rank, subgroup, level, class, fileName, zone, online = GetRaidRosterInfo(i);
			
			local flagHaveinRaid=false;
			totalmember=getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"]);
			for j=1,totalmember,1 do
				Uasit_Debug("sPlayer"..sPlayer,name,TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["player"]);
				if(sPlayer==TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["player"]) then
					
					if((not TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"] ) and online) then
						TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["zone"]=zone;	
						TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["totaltime"]=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["totaltime"]+time()-TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"];
						TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"]=0;
					end
					if((not TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"] ) and (not online)) then
						
						TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["totaltime"]=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["totaltime"]+time()-TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"];
						TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"]=time();
					end
					
					if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"]  and (not online)) then		
						TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"]=time();
					end
					
					if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"]  and  online) then		
						TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["zone"]=zone;	
					end

					TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"]=online;
					flagHaveinRaid=true;
				end	
			end
			local t = date("*t");
			
			if (not flagHaveinRaid) then
				
				if ( not online ) then
					table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"],1,
						{
							["player"] = sPlayer,
							["race"] = UdkpRaid_Race_Cn[race],
							["class"] = UdkpRaid_Class_Cn[fileName],
							["level"] = level,
							["join"] = t.hour..":"..t.min,
							["totaltime"] = time()-TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["IntStartTime"],
							["dkp"] = 0,
							["ldkp"] = 0,
							["pdkp"] = getUdkpPlayerDKPLeft(sPlayer),
							["zone"] = zone,
							["rank"] = rank,
							["online"] = online,
							["leavetime"] = time()
						}
					);
					--UdkpRaid_Offline[name] = true;
				else
					table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"],1,
						{
							["player"] = sPlayer,
							["race"] = UdkpRaid_Race_Cn[race],
							["class"] = UdkpRaid_Class_Cn[fileName],
							["level"] = level,
							["join"] = t.hour..":"..t.min,
							["totaltime"] = time()-TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["IntStartTime"],
							["dkp"] = 0,
							["ldkp"] = 0,
							["pdkp"] = getUdkpPlayerDKPLeft(sPlayer),
							["zone"] = zone,
							["rank"] = rank,
							["online"] = online,
							["leavetime"] = 0
						}
					);
				end
			end
			
		end
	end
	totalmember=getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"]);
	for j=1,totalmember,1 do
		memberInRaid=false;
		for i = 1, GetNumRaidMembers(), 1 do
			local sPlayer = UnitName("raid" .. i);
			
			if(sPlayer==TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["player"]) then
				memberInRaid=true;
				break;
			end
		end
		if(memberInRaid==false) then
		
			if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"]==1) then
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"]=time();
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"]=0;
			else
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["totaltime"]=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["totaltime"]+time()-TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"];
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["leavetime"]=time();
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][j]["online"]=0;
			end
		end
		
	end
	Uasit_UpdateMember();

end

function UdkpAddDkp()
	if(CurrentRaidId==0) then return; end
	player=getglobal("UdkpAddMember"):GetText();
	dkpDesc=getglobal("UdkpAddDkpFrameDes"):GetText();
	dkppoint=getglobal("UdkpAddDkpFramePoint"):GetNumber();
	
	Uasit_Debug(dkppoint,player,dkpDesc);
	
	if(dkppoint==0) then
	UdkpRaidMessageBox(UDKP_MESSAGE_DKP_ISNULL) ;
	return;
	end
	if(dkpDesc=="") then 
	dkpDesc=UDKP_STRING_DKP_NODES;
	end
	totalMembers=0;
	if(getn(TEST_PARTY_STUFF) > 0) then
			totalMembers = getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"]);
			
			if(totalMembers<1) then
				UdkpRaidMessageBox(UDKP_MESSAGE_DKP_NOMEMBER);
				return;
			end
	else
		return;
	end
	eventid=UdkpGenId();
	local udkptype=2;
	if(player=="0" or player=="1") then
		udkptype=1
	end
	table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"],1,
					{
						["eventid"] = eventid,
						["itemid"] = "",
						["itemname"] = "",
						["reason"] = dkpDesc,
						["type"] = udkptype,
						["eventtime"] = Udkp_Date(),
						["dkp"] = dkppoint,
						["players"]={}
					}
				);
	
	if(player=="0") then --add online
		for i=1, totalMembers  do
			if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["online"]==1) then
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["dkp"]=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["dkp"]+dkppoint;
				table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][1]["players"],1,TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["player"]);	
			end
		end
		
	elseif(player=="1") then --add all
		for i=1, totalMembers  do
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["dkp"]=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["dkp"]+dkppoint;
				table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][1]["players"],1,TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["player"]);	
		end
		
	else --add one member
		for i=1, totalMembers  do
			if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["player"]==player) then
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["dkp"]=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["dkp"]+dkppoint;
				table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][1]["players"],1,player);
			end	
		end
		
	end
	Uasit_UpdateMember();
--	Uasit_UpdateEvent();
	
end

function UdkpdeleteMember()
	--delete membe
	deleteMemberName=getglobal("UdkpAddMember"):GetText();
	
	totalMembers = getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"]);
	for i=totalMembers, 1,-1  do	
		
		if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["player"]==deleteMemberName) then
		Uasit_Debug(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["player"],deleteMemberName);
			table.remove(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"],i);
		end
	end
	--delete event about this person
	totalEvents = getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"]);
	
	for i=totalEvents, 1,-1  do
		totalplayers=getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["players"]);
		flag=false;
		for j=totalplayers,1,-1 do
			--event is for one member

			if( not flag) then
				if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["players"][j]==deleteMemberName and totalplayers==1) then
					table.remove(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"],i);
					flag=true;
				elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["players"][j]==deleteMemberName) then
					table.remove(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["players"],j);
				end
			end
		end
	end
	--delete item
	local totalitem=getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Items"]);
	for i=totalitem, 1,-1  do
		
		if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Items"][i]["player"]==deleteMemberName) then
			table.remove(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Items"],i)
		end
		
	end

	Uasit_UpdateMember();
--	Uasit_UpdateEvent();

end


function UdkpMemberEventListFrame_OnMouseUp()

	if ( UdkpMemberEventListFrame.isMoving ) then
		UdkpMemberEventListFrame:StopMovingOrSizing();
		UdkpMemberEventListFrame.isMoving = false;
	end
end

function UdkpMemberEventListFrame_OnMouseDown(button)
	if ( ( ( not UdkpMemberEventListFrame.isLocked ) or ( UdkpMemberEventListFrame.isLocked == 0 ) ) and ( arg1 == "LeftButton" ) ) then
		UdkpMemberEventListFrame:StartMoving();
		UdkpMemberEventListFrame.isMoving = true;
	end
end



function UdkpMemberEventListFrame_OnHide()
	if ( UdkpMemberEventListFrame.isMoving ) then
		UdkpMemberEventListFrame:StopMovingOrSizing();
		UdkpMemberEventListFrame.isMoving = false;
	end
end

function UdkpMemberEventListFrame_OnShow()
	
	player=getglobal("UdkpAddMember"):GetText();
	Uasit_Debug(player);
	if(player=="") then return end
	local totalevents=getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"]);
	local memberevents=0;
	for i=1,UDKP_NUM_BIDS_TO_DISPLAY,1 do
		getglobal("UdkpMemberEventFrameListButton"..i):Hide();
	end
	
	if(totalevents>0) then
		for i=1,totalevents,1 do
			
			totalplayers=getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["players"]);
	
			for j=1,totalplayers, 1 do

				if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["players"][j]==player) then
					memberevents=memberevents+1;
					local offset = FauxScrollFrame_GetOffset(UdkpMemberEventListScrollFrame);
					if(memberevents>offset) then
						if(UDKP_NUM_BIDS_TO_DISPLAY>=(memberevents - offset)) then
							--k=j+offset;
							getglobal("UdkpMemberEventFrameListButton" .. (memberevents -offset) .. "EventName"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["reason"]);
							getglobal("UdkpMemberEventFrameListButton" .. (memberevents -offset) .. "EventItem"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["itemname"]);
							getglobal("UdkpMemberEventFrameListButton" .. (memberevents -offset) .. "EventDkp"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Events"][i]["dkp"]);
							getglobal("UdkpMemberEventFrameListButton" .. (memberevents -offset) .. "EventMember"):SetText(player);
							
							getglobal("UdkpMemberEventFrameListButton"..(memberevents -offset)):Show();
						end
					end
					
					break;
				end
			end
		end
		FauxScrollFrame_Update(UdkpMemberEventListScrollFrame,memberevents ,UDKP_NUM_BIDS_TO_DISPLAY,AUCTIONS_BUTTON_HEIGHT);
	
	
	end
end


function Udkp_CompareDkps(a1, a2)

	local  method, way = Udkp_SortOptions["method"], Udkp_SortOptions["itemway"];

	if ( method == "name" ) then
		local c1, c2 = a1["player"], a2["player"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "class" ) then
		local c1, c2 = a1["class"], a2["class"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "dkp" ) then
		local c1, c2 = a1["dkp"], a2["dkp"] ;
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "pdkp" ) then
		local c1, c2 = a1["pdkp"], a2["pdkp"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "ldkp" ) then
		local c1, c2 = a1["dkp"]+a1["pdkp"], a2["dkp"]+a2["pdkp"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "jointime" ) then
		local c1, c2 = a1["join"], a2["join"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "totaltime" ) then
		local c1, c2 = a1["totaltime"], a2["totaltime"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "zone" ) then
		local c1, c2 = a1["zone"], a2["zone"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "level" ) then
		local c1, c2 = a1["level"], a2["level"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	elseif ( method == "online" ) then
		local c1, c2 = a1["level"], a2["level"];
		if ( way == "asc" ) then
			return c1 < c2;
		else
			return c1 > c2;
		end
	
	end
end


function Udkp_SortDkp()

		table.sort(
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"],
			Udkp_CompareDkps
		);

end

function UdkpSortOptions_ToggleDkp(method)

	if ( Udkp_SortOptions.method and Udkp_SortOptions.method == method ) then
		if ( Udkp_SortOptions.itemway and Udkp_SortOptions.itemway == "asc" ) then
			Udkp_SortOptions.itemway = "desc";
		else
			Udkp_SortOptions.itemway = "asc";
		end
	else
		Udkp_SortOptions.method = method;
		Udkp_SortOptions.itemway = "asc";
	end
	Udkp_SortDkp();
	Uasit_UpdateMember();
end



function getUdkpPlayerDKPLeft(sPlayer)
	if(UDKP_MEMBER_DKP) then
	
		if( CurrentRaidId~=0) then
			if(UDKP_MEMBER_DKP) then
				totalteamnumber=getn(UDKP_MEMBER_DKP);
				for i=1,totalteamnumber do
					if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"]==UDKP_MEMBER_DKP[i]["teamid"]) then
						totalmember=getn(UDKP_MEMBER_DKP[i]["Members"]);
						for j=1,totalmember do
							if(UDKP_MEMBER_DKP[i]["Members"][j]["player"]==sPlayer) then
								return UDKP_MEMBER_DKP[i]["Members"][j]["dkp"]
							end
						end
					end
				end
			end
			
		end
	
	end
	return "0";
end